package com.br.edufindme;

import com.br.edufindme.R;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;


public class SendMessageActivity extends Activity {
	
	
	
	TextView txtEmailSendTo, txtEmailSendSubject,txtEmailSendTopMenuFirst ,txtEmailSendTopMenuMid, txtEmailSendTopMenuEnd, txtEmailSendTitulo, txtDialogTitle, txtDialogBody;
	MultiAutoCompleteTextView multiCompleteEmailSendTo, AutoCompleteEmailSendSubject;
	ImageView IconDialog;
	EditText edtEmailSendMessage, edtEmailSendSubject;
	Button btEmailSendTopMenuleft, btEmailSendButtonMenuRight, btDialog;
	MessageActivity Backpressed;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_newmessage);
		
		objetosInicializados();
		
		fontePersonalizada();
		
		autoComplete();
      
		clickListener();
	}
	
	public void objetosInicializados(){
		try{
			btEmailSendTopMenuleft = (Button) findViewById(R.id.btEmailSendTopMenuleft);
			btEmailSendButtonMenuRight = (Button) findViewById(R.id.btEmailSendButtonMenuRight);
			
			//txtEmailSendTo = (TextView) findViewById(R.id.txtEmailSendTo);
			txtEmailSendSubject = (TextView) findViewById(R.id.txtEmailSendSubject);
			txtEmailSendTopMenuFirst = (TextView) findViewById(R.id.txtEmailSendTopMenuFirst);
			txtEmailSendTopMenuMid = (TextView) findViewById(R.id.txtEmailSendTopMenuMid);
			txtEmailSendTopMenuEnd = (TextView) findViewById(R.id.txtEmailSendTopMenuEnd);
			txtEmailSendTitulo = (TextView) findViewById(R.id.txtEmailSendTitulo);
			
			edtEmailSendMessage = (EditText) findViewById(R.id.edtEmailSendMessage);
			edtEmailSendSubject = (EditText) findViewById(R.id.edtEmailSendSubject);
			
			multiCompleteEmailSendTo = (MultiAutoCompleteTextView) findViewById(R.id.multiCompleteEmailSendTo);
		}catch(Exception erro){
			Log.i(SendMessageActivity.class.getName(), "objetosIncializados: " + erro);
		}
	}
	public void fontePersonalizada(){
		try{
			//Tipo da fonte
			Typeface font = Typeface.createFromAsset(getAssets(), "GothamRnd-Medium.otf");
			txtEmailSendTitulo.setTypeface(font);
			txtEmailSendTopMenuFirst.setTypeface(font);
			txtEmailSendTopMenuMid.setTypeface(font);
			txtEmailSendTopMenuEnd.setTypeface(font);
			
			SpannableString content = new SpannableString("FIND");
			content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
			txtEmailSendTopMenuMid.setText(content);
		}catch(Exception erro){
			Log.i(SendMessageActivity.class.getName(), "fontePersonalizada: " + erro);
		}
	}
	
	public void autoComplete(){
		try{
			String[] array = { "San Diego", "San Diego1", "San Diego2", "San Diego3", "San Diego4", "San Diego5", "San Diego6"};
			
	        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	                android.R.layout.select_dialog_item, array);

			multiCompleteEmailSendTo.setAdapter(adapter);
			multiCompleteEmailSendTo.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
		}catch(Exception erro){
			Log.i(SendMessageActivity.class.getName(), "autoComplete: " + erro);
		}
	}
	public void clickListener(){
		try{
			multiCompleteEmailSendTo.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					Log.i("CLICOU","MULTI");
					
				}
			});
			
			btEmailSendTopMenuleft.setOnClickListener(new OnClickListener() {			
				@Override
				public void onClick(View v) {
					try{
					onBackPressed();				
					}catch(Exception erro){
						Log.i(SendMessageActivity.class.getName(), "btMailSendTopMenuleft: " + erro);
					}
					}
					
			});
			
			btEmailSendButtonMenuRight.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					String multi = multiCompleteEmailSendTo.getText().toString();

					try{
					if(multi.equals("")){
						messageDialog(R.string.titleSendTo,R.string.messageSendTo, R.drawable.ic_alert);
					}else{
						messageDialog(R.string.titleSend,R.string.messageSend, R.drawable.accept);
						multiCompleteEmailSendTo.setText("");
						edtEmailSendMessage.setText("");
						edtEmailSendSubject.setText("");
					}
					}catch(Exception erro){
						Log.i(SendMessageActivity.class.getName(), "btEmailSendButtonMenuRight: " + erro);
					}
				}
			});
		}catch(Exception erro){
			Log.i(SendMessageActivity.class.getName(),"clickListener: " + erro);
		}
		
	}
	
	public void messageDialog(int title, int body, int icon) {

		final Dialog message = new Dialog(this);
		message.requestWindowFeature(Window.FEATURE_NO_TITLE);
		message.setContentView(R.layout.activity_dialog);

		txtDialogTitle = (TextView) message.findViewById(R.id.txtDialogTitle);
		txtDialogBody = (TextView) message.findViewById(R.id.txtDialogBody);
		IconDialog = (ImageView) message.findViewById(R.id.imageDialog);
		btDialog = (Button) message.findViewById(R.id.btDialog);

		txtDialogTitle.setText(title);
		txtDialogBody.setText(body);
		IconDialog.setImageResource(icon);
		
		btDialog.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				message.dismiss();
			}
		});
		message.show();
	}
}
